﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using LevelEditor.Model.Entity;
using System.Collections;
using LevelEditor.Model.Enum;
using LevelEditor.Model.IO;

namespace LevelEditor.View.UserControls
{
    /// <summary>
    /// Logique d'interaction pour GameSpriteEditor.xaml
    /// </summary>
    public partial class GameSpriteEditor : UserControl
    {
        public event EventHandler GameSpriteChanged;

        public void OnGameSpriteChanged(GameSprite gameSprite){
            if( GameSpriteChanged != null)
                GameSpriteChanged(gameSprite, new EventArgs());
            
        }

        private GameSprite gameSprite;

        public List<SpriteSheet> SpriteSheets {get;set;} 

        public GameSpriteEditor()
        {
            InitializeComponent();

            
            this.name.KeyUp += new KeyEventHandler(name_KeyUp);
            this.category.KeyUp += new KeyEventHandler(category_KeyUp);
            this.spriteType.SelectionChanged += new SelectionChangedEventHandler(spriteType_SelectionChanged);

            this.team.KeyUp += new KeyEventHandler(team_KeyUp);
            this.isMovable.Checked += new RoutedEventHandler(isMovable_Checked);
            this.isSelectable.Checked += new RoutedEventHandler(isSelectable_Checked);

            spriteSheetsCB.ItemsSource = SpriteSheetIO.LoadSpriteSheets();
        }

        public GameSprite GameSprite 
        {
            set
            {
                if (gameSprite == value) return; //Or use a dirty flag

                gameSprite = value;
                LoadInfos();
            }
            get
            {
                return gameSprite;
            }
        }

        private void LoadInfos()
        {
            if (gameSprite == null) return;

            name.Text = gameSprite.Name;
            category.Text = gameSprite.Category;
            filename.Text = gameSprite.ImageSource.OriginalString.Split(new char[] { '/' }).Last();
            spriteType.SelectedIndex = (int)gameSprite.AssetType;

            if (gameSprite.AssetType == AssetType.ACTOR_SPRITE)
            {
                ActorSprite actor = null;
                if (gameSprite is ActorSprite)
                {
                    actor = gameSprite as ActorSprite;
                }
                else
                {
                    //Convert to ActorSprite if not already a ActorSprite
                    actor = new ActorSprite();
                    actor.ID = gameSprite.ID;
                    actor.AssetType = gameSprite.AssetType;
                    actor.BoundaryColor = Colors.Red;
                    actor.Category = gameSprite.Category;
                    actor.Fill = SpriteFill.None;
                    actor.IsSelectable = true;
                    actor.IsMovable = true;
                    actor.ImageSource = gameSprite.ImageSource;
                    actor.Name = gameSprite.Name;

                    actor.Animations = new List<Animation>();
                    actor.Animations.Add(new Animation() { AnimationType = AnimationEnum.WALKING_LEFT, Name = "Walking Toward Left", FrameRate = 30 });
                    actor.Animations.Add(new Animation() { AnimationType = AnimationEnum.WALKING_RIGHT, Name = "Walking Toward Right", FrameRate = 30 });
                    actor.Animations.Add(new Animation() { AnimationType = AnimationEnum.FALLING, Name = "Falling Down", FrameRate = 30 });
                    actor.Animations.Add(new Animation() { AnimationType = AnimationEnum.TALKING, Name = "Talking", FrameRate = 30 });
                    actor.Animations.Add(new Animation() { AnimationType = AnimationEnum.JUMPING, Name = "Jumping", FrameRate = 30 });
                    actor.Animations.Add(new Animation() { AnimationType = AnimationEnum.SLEEPING, Name = "Sleeping", FrameRate = 30 });
                    actor.Animations.Add(new Animation() { AnimationType = AnimationEnum.IDLE, Name = "Waiting", FrameRate = 30 });

                    gameSprite = actor;
                }

                Rules.Visibility = Visibility.Visible;
                Behaviours.Visibility = Visibility.Visible;

                team.Text = actor.Team;
                isSelectable.IsChecked = actor.IsSelectable;
                isMovable.IsChecked = actor.IsMovable;
                
                behavioursDG.ItemsSource = null;
                behavioursDG.ItemsSource = actor.Animations;
            }
            else
            {
                Rules.Visibility = Visibility.Collapsed;
                Behaviours.Visibility = Visibility.Collapsed;
            }
        }

        private void ClearForm()
        {
            name.Text = "";
            category.Text = "";
            filename.Text = "";
            spriteType.SelectedIndex = 0;

            Rules.Visibility = Visibility.Collapsed;
            Behaviours.Visibility = Visibility.Collapsed;
        }

        //Event Handlers

        protected void spriteType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (gameSprite == null || (int)gameSprite.AssetType == spriteType.SelectedIndex)
                return;

            gameSprite.AssetType = (AssetType)Enum.ToObject(typeof(AssetType), spriteType.SelectedIndex);

            LoadInfos();

            OnGameSpriteChanged(gameSprite);
        }

        void isSelectable_Checked(object sender, RoutedEventArgs e)
        {
            (gameSprite as ActorSprite).IsSelectable = (bool)isSelectable.IsChecked;
            OnGameSpriteChanged(gameSprite);
        }

        void isMovable_Checked(object sender, RoutedEventArgs e)
        {
            (gameSprite as ActorSprite).IsSelectable = (bool)isMovable.IsChecked;
            OnGameSpriteChanged(gameSprite);
        }

        void team_KeyUp(object sender, KeyEventArgs e)
        {
            (gameSprite as ActorSprite).Team = team.Text;
            OnGameSpriteChanged(gameSprite);
        }

        void category_KeyUp(object sender, KeyEventArgs e)
        {
            gameSprite.Category = category.Text;
            OnGameSpriteChanged(gameSprite);
        }

        void name_KeyUp(object sender, KeyEventArgs e)
        {
            gameSprite.Name = name.Text;
            OnGameSpriteChanged(gameSprite);
        }

    }
}
